package com.wty.good.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wty.commons.id.IdWorker;
import com.wty.good.dto.GoodInfoDtoWty;
import com.wty.good.mapper.GoodDetailsMapperWty;
import com.wty.good.mapper.GoodImageMapperWty;
import com.wty.good.mapper.GoodMapperWty;
import com.wty.good.model.DataGridWty;
import com.wty.good.model.GoodInfoWty;
import com.wty.good.pojo.GoodDetailsWty;
import com.wty.good.pojo.GoodImageWty;
import com.wty.good.pojo.GoodWty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author:wangtianyu
 * @Date：Created on 2020/4/23 0023 13:04
 */
@Service
public class GoodServiceWty {

    @Autowired
    private GoodMapperWty goodMapper;

    @Autowired
    private GoodDetailsMapperWty goodDetailsMapper;

    @Autowired
    private GoodImageMapperWty goodImageMapper;

    @Autowired
    private IdWorker idWorker;

    /*查询商品明细*/
    public List<GoodInfoWty> selectListByPage(String word, Long page, Long count) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("word", word == null ? "%%" : "%" + word + "%");
        map.put("page",page);
        map.put("count",count);
        List<GoodInfoWty> goodInfos = goodMapper.selectGoodInfosByPage(map);
        return goodInfos;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public GoodWty addGood(GoodWty good) throws Exception {
        good.setGoodid(idWorker.nextId());
        good.setGoodstate(0);
        Date date = new Date();
        good.setCreatetime(date);
        good.setUpdatetime(date);
        int count = goodMapper.insert(good);
        if (count == 0) {
            throw new RuntimeException();
        }
        return good;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public GoodInfoWty addGoodInfo(GoodInfoDtoWty info) throws Exception {
        GoodDetailsWty detail = new GoodDetailsWty();
        BeanUtils.copyProperties(info, detail);
        int count = goodDetailsMapper.insert(detail);
        if (count == 0) {
            throw new RuntimeException();
        }
        List<GoodImageWty> goodImages = new ArrayList<>();

        String[] split = info.getImagepaths().split(","); // 对图片路径进行分割

        for (int i = 0; i < split.length; i++) {
            GoodImageWty image = new GoodImageWty();
            image.setImageid(idWorker.nextId());
            image.setGoodid(info.getGoodid());
            image.setImagepath(split[i]);
            goodImages.add(image);
        }
        for (int i = 0; i < goodImages.size(); i++) {
            goodImageMapper.insert(goodImages.get(i));
        }
        GoodWty good = goodMapper.selectById(info.getGoodid());
        GoodInfoWty goodInfo = new GoodInfoWty();
        BeanUtils.copyProperties(good, goodInfo);
        goodInfo.setGoodDetails(detail);
        goodInfo.setGoodImages(goodImages);
        return goodInfo;
    }

    public DataGridWty<GoodWty> selectGoodList(String word, Long page, Long count) throws Exception{
        DataGridWty<GoodWty> dataGrid = new DataGridWty<>();
        Page<GoodWty> p = new Page<>(page,count);
        QueryWrapper<GoodWty> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("goodtitle",word==null?"":word);
        IPage<GoodWty> goodIPage = goodMapper.selectPage(p, queryWrapper);
        dataGrid.setTotal(goodIPage.getTotal());
        dataGrid.setPages(goodIPage.getPages());
        dataGrid.setList(goodIPage.getRecords());
        return dataGrid;
    }

    public GoodInfoWty selectGoodInfoById(Long goodid)throws Exception {
        return goodMapper.selectGoodInfoById(goodid);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public Integer upGoods(List<Long> goodids)throws Exception {
        int count = 0;
        for (int i = 0; i < goodids.size(); i++) {
            GoodWty good = new GoodWty();
            good.setGoodid(goodids.get(i));
            good.setGoodstate(1);
            goodMapper.updateById(good);
            count++;
        }
        return count;
    }
}
